package com.goddess.yeecolor.wxapi;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.goddess.yeecolor.activity.EnrollFinishAcitity;
import com.goddess.yeecolor.activity.myFragmentItemActivityAll.JoinSuccessActivity;
import com.goddess.yeecolor.tools.Constans;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static com.goddess.yeecolor.R.id.state;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constans.App_ID);
        api.handleIntent(getIntent(), this);
        finish();
        overridePendingTransition(0,0);

    }

    private void  setBroad(){
        Intent intent=new Intent();
        intent.putExtra("data","关闭");
        intent.setAction("com.zy.brocast");
        sendBroadcast(intent);
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);

    }
    @Override
    public void onReq(BaseReq req) {

    }

    private int result;
    @Override
    public void onResp(BaseResp resp) {


        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String msg="";
             result=resp.errCode;

            switch (result){
                case 0:
                    msg="支付成功";
                    notifity();
                    break;
                case -2:
                    msg="取消支付";
                    finish();
                    break;
                default:

                    msg="支付失败";
                    notifity();
                    break;
            }


        }

    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0,0);
    }

    /**
     * 通知商户
     * @param result
     */

    private void notifity(){
        Map<Object,Object >map=new HashMap<>();
        map.put("order_id", SharePreceUtils.getOther("order","order_id"));
        map.put("unique",SharePreceUtils.getOther("user","unique"));
        SharePreceUtils.saveString("order","join","tag");
        if (SharePreceUtils.getOther("order","tag").equals("join")){
            OkttpUtils.getIntence().OkttpPost(MyUrl.JOINSHOW,map,handler,1);
        }
        else{
            OkttpUtils.getIntence().OkttpPost(MyUrl.WXPAYRESULT,map,handler,1);
        }

    }


    private String state;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                String data=msg.getData().getString("result");

                JSONObject object=new JSONObject(data);
                if (object.getInt("code")==100){
                    JSONObject resutl=object.getJSONObject("result");
                    state=resutl.getString("state");
                    if (state.equals("1")){
                        ToastUtils.setToastText("支付成功", WXPayEntryActivity.this);
                    }
                }
                else {
                    JSONObject resutl=object.getJSONObject("result");
                    state=resutl.getString("state");
                    if (state.equals("0")){
                        ToastUtils.setToastText("支付失败", WXPayEntryActivity.this);
                    }
                }
                String tag=SharePreceUtils.getOther("orders","tag");
                if (tag.equals("reus")){
                    JumpIntents.startIntent(WXPayEntryActivity.this, EnrollFinishAcitity.class,state);//这是活动购买选中微信的支付按钮/
                    setBroad();//关闭支付界面
                }
                else if (tag.equals("join")){
                    JumpIntents.startIntent(WXPayEntryActivity.this, JoinSuccessActivity.class,state);//这是加入优雅荟选中微信的支付按钮/
                    setBroad();//关闭支付界面
                }


            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };


}